﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;
using System.Collections;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class AssociationExpensesViewModel : IViewModel
    {
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdminRepository;
        /// <summary>
        /// 
        /// </summary>
        IFinanceService _FinanceService;
        /// <summary>
        /// Initializes a new instance of the <see cref="AssociationExpensesViewModel"/> class.
        /// </summary>
        /// <param name="administrationRepository">The administration repository.</param>
        /// <param name="financeService">The finance service.</param>
        public AssociationExpensesViewModel(IAdministrationRepository administrationRepository, IFinanceService financeService)
        {
            _AdminRepository = administrationRepository;
            _FinanceService = financeService;
        }

        /// <summary>
        /// Gets the association.
        /// </summary>
        /// <returns></returns>
        public ICommittee GetAssociation()
        {
            return _AdminRepository.GetCommitteeByID(RegistryInfo.CommitteeID);
        }

        /// <summary>
        /// Gets the committee balance.
        /// </summary>
        /// <returns></returns>
        public double GetCommitteeBalance()
        {
            return _FinanceService.GetCommitteeBalance(RegistryInfo.CommitteeID);
        }
        /// <summary>
        /// Withes the draw from committee.
        /// </summary>
        /// <param name="amount">The amount.</param>
        /// <param name="date">The date.</param>
        /// <param name="checkNum">The check num.</param>
        /// <param name="notes">The notes.</param>
        /// <param name="projectID">The project ID.</param>
        public void WithDrawFromCommittee(double amount, DateTime date, string checkNum, string notes, Guid projectID)
        {
            using (IUnitOfWork trans = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                var currentCommittee = _AdminRepository.GetCommitteeByID(RegistryInfo.CommitteeID);
                ICommitteeBankAccount committeeBankAccount = currentCommittee.CommitteeBankAccounts.Where(commBA => commBA.ProjectID.Equals(projectID)).First();
                ICommitteeBankAccountExpense commExpense = ServiceLocator.Current.GetInstance<ICommitteeBankAccountExpense>();
                commExpense.CommitteeBankAccountExpensesID = Guid.NewGuid();
                commExpense.CommitteeBankAccountID = committeeBankAccount.CommitteeBankAccountID;
                commExpense.WithdrawAmount = amount;
                commExpense.WithdrawCheckNumber = checkNum;
                commExpense.WithdrawDate = date;
                commExpense.Notes = notes;
                committeeBankAccount.Balance = committeeBankAccount.Balance.GetValueOrDefault(0) - amount;
                _AdminRepository.CreateCommitteeBankAccountExpense(commExpense);
                trans.Commit();
            }

        }


        /// <summary>
        /// Gets the user project ID.
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetUserProjects()
        {
            return _AdminRepository.GetUserProjects(RegistryInfo.UserID);
        }
    }
}
